package product;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class TypeofTopthree {

	/**统计排名前3的省共同拥有的农产品类型
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		if (args.length != 2) {
			System.err.println("path err!");
			System.exit(-1);
		}
		Job job = new Job(new Configuration(), "sortby");
		job.setJarByClass(TypeofTopthree.class);

		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		job.setMapperClass(stmaps.class);
		job.setReducerClass(streduce.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		job.waitForCompletion(true);

	}
	public static class stmaps extends Mapper<LongWritable,Text,Text,Text>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("\t");
			if (lines.length==6) {
				String type = lines[0].trim();
				String provice = lines[4].trim();
				context.write(new Text(provice), new Text(type));
			}
		};
	}
	//shuffle: <provice,{name1,name2,name3....}>
	public static class streduce extends Reducer<Text,Text,Text,Text>{
		TreeMap<Integer, String> tMap=new TreeMap<>(
				new Comparator<Integer>() {
					@Override
					public int compare(Integer o1, Integer o2) {
						return o2.compareTo(o1);
					}
				}
				);
		//sort size through TreeMap.
		protected void reduce(Text k2, java.lang.Iterable<Text>values, org.apache.hadoop.mapreduce.Reducer<Text,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			HashSet<String> type=new HashSet<>();
			for (Text cnt : values) {
				type.add(cnt.toString()+"\t");
			}
			int pall=type.size();
			tMap.put(pall, type.toString());
		};
		
		protected void cleanup(org.apache.hadoop.mapreduce.Reducer<Text,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			if (tMap.size()>3) {
				tMap.remove(tMap.lastKey());
			}
			ArrayList<String> list=new ArrayList<>();
			Set<Integer> keySet = tMap.keySet();
			for (Integer info : keySet) {
				String p=tMap.get(info);
				list.add(p);
			}
			
			String[] array0 = list.get(0).toString().split("\t");
			String[] array1 = list.get(1).toString().split("\t");
			String[] array2 = list.get(2).toString().split("\t");
			for (int i = 0; i < array0.length; i++) {
				String t0=array0[i];
				for (int j = 0; j < array1.length; j++) {
					String tt0=array0[j];
					for (int k = 0; k < array2.length; k++) {
						String ttt0=array2[k];
						if (t0==tt0 && tt0==ttt0) {
							context.write(new Text(ttt0),new Text("---"));
						}
					}
				}
			}
			
		};
	}
	

}
